package com.tjshop.android.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.tjshop.android.R;
import com.tjshop.android.adapter.CategoryListAdapter;
import com.tjshop.android.constants.UrlConfigs;
import com.tjshop.android.model.tj.json.Category;
import com.tjshop.android.util.WaitProgressDialog;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FragmentCatagory extends Fragment implements Handler.Callback {

	private Context mContext;
	private Handler handler;
	private ArrayList<Category> categoryList = new ArrayList<Category>();

	public FragmentCatagory() {
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mQueue = Volley.newRequestQueue(getActivity());
		mProgressDialog = new WaitProgressDialog(getActivity());
		

	}

	ListView listView;
	TextView nearBy_category;
	private RequestQueue mQueue;
	private WaitProgressDialog mProgressDialog;

	// 初始化标题栏
	void initTitleBar(final View mContainer) {
		listView = (ListView) mContainer
				.findViewById(R.id.fragment_catagory_list);
	}

	private void initView() {
		listView.setAdapter(new CategoryListAdapter(mContext, categoryList));
	}

	@Override
	public View onCreateView(LayoutInflater inf, ViewGroup container,
			Bundle savedInstanceState) {
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.activity_main_fragment_catagory, null);
		mContext = this.getActivity();
		handler = new Handler(this);
		getAllCategory();
		initTitleBar(v);

		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onPause() {
		super.onPause();

		boolean userVisibleHint = getUserVisibleHint();
		Log.w(FragmentCatagory.class.getSimpleName(),
				"onPause - userVisibleHint: " + userVisibleHint);
	}

	@Override
	public void onResume() {
		super.onResume();

		boolean userVisibleHint = getUserVisibleHint();
		Log.w(FragmentCatagory.class.getSimpleName(),
				"onResume - userVisibleHint: " + userVisibleHint);
	}

	void getAllCategory() {
		mProgressDialog.show("获取分类信息中", true, false);

		StringRequest stringRequest = new StringRequest(Method.GET,
				UrlConfigs.URL_GET_ALL_CATAGORY,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						JSONArray jsonU;
						try {
							JSONObject obj = new JSONObject(response);
							if (obj.getInt("r") == 0) {
								jsonU = obj.getJSONArray("s");
								for (int i = 0; i < jsonU.length(); i++) {
									Category c = new Category();
									c.parse(jsonU.getJSONObject(i));
									categoryList.add(c);
								}
								Message message = new Message();
								message.what = 2;
								handler.sendMessage(message);
							} else {
								Message message = new Message();
								message.what = 1;
								handler.sendMessage(message);
							}

						} catch (JSONException e) {
							Message message = new Message();
							message.what = 1;
							handler.sendMessage(message);
							e.printStackTrace();
						}

					}
				}, new Response.ErrorListener() {

					@Override
					public void onErrorResponse(VolleyError error) {
						Message message = new Message();
						message.what = 1;
						handler.sendMessage(message);

					}

				});

		mQueue.add(stringRequest);
	}

	Map<String, Boolean> isSuccess = new HashMap<String, Boolean>();

	@Override
	public boolean handleMessage(Message message) {
		switch (message.what) {
		case 1:
			Toast.makeText(mContext, "网络忙，请稍后再试！", Toast.LENGTH_SHORT).show();
			break;
		case 2:
			mProgressDialog.dismiss();
			Toast.makeText(mContext, "获取分类成功！", Toast.LENGTH_SHORT).show();
			initView();
			break;
		case 100:
			break;
		default:
			break;
		}
		return false;
	}

}
